﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DoeLibsMVC.Models;
using DoeObjects;
using DoeLibsMVC.Security;

namespace DoeLibsMVC.Controllers
{
    public class LoanableController : BaseController
    {
        //
        // GET: /Loanable/

        public ActionResult Index()
        {
            return View();
        }

        [Authorize(Roles = UserCategoryHelper.STAFF_CATEGORY)]
        [HttpGet]
        public ActionResult Add()
        {
            EditLoanableModel elm = new EditLoanableModel();
            elm.Location = User.Room;

            return View(elm);
        }

        //[Authorize(Roles = UserCategoryHelper.STAFF_CATEGORY)]
        //[HttpPost]
        //public ActionResult Add(EditLoanableModel model)
        //{
        //    if (string.IsNullOrEmpty(model.ISBN10) && string.IsNullOrEmpty(model.ISBN13))
        //    {
        //        ModelState.AddModelError("ISBN10", "At least one ISBN number must be specified.");
        //        ModelState.AddModelError("ISBN13", "At least one ISBN number must be specified.");
        //    }

        //    if (ModelState.IsValid)
        //    {
        //        //Creates a new title if its not found
        //        Title title = null;
        //        if ((title = Title.getByIsbn10(model.ISBN10)) == null && (title = Title.getByIsbn13(model.ISBN13)) == null)
        //        {
        //            title = new Title();
        //            title.BookTitle = model.Title;
        //            title.ISBN10 = model.ISBN10;
        //            title.ISBN13 = model.ISBN13;
        //            title.EditionNumber = model.Edition;
        //            title.EditionYear = model.PublicationYear;
        //            title.FirstEditionYear = model.FirstEditionYear;
        //            title.Publisher = model.Publisher;
                    

        //            //save only title, if publisher could be created
        //            if (title.Publisher.PublisherId > 0)
        //            {
        //                if (title.save())
        //                {
        //                    foreach (Author a in model.Authors)
        //                    {
        //                        Title.addAuthor(a.Aid, title.TitleId);
        //                    }
        //                }
        //            }
        //        }

        //        if (title != null && title.TitleId > 0) //only if the title was saved successfuly
        //        {
        //            //Create the loanable and link it
        //            Loanable loanable = new Loanable();

        //            loanable.Barcode = model.Barcode;
        //            loanable.Category = model.Category;
        //            loanable.Location = model.Location;
        //            loanable.Owner = this.User;
        //            loanable.Title = title;

        //            if (loanable.save())
        //            {
        //                base.Message = new HttpMessage("Loanable was added.", true);

        //                return RedirectToAction("Get", "Title", new { id = title.TitleId });
        //            }
        //            else
        //            {
        //                base.Message = new HttpMessage("Loanable could not be added.", false);
        //            }
        //        }
        //        else
        //        {
        //            base.Message = new HttpMessage("Loanable could not be added because it could not be linked with the title.", false);
        //        }
        //    }

        //    return View(model);
        //}
    }
}
